package com.autumn.zero.workflow.application.services.impl;

import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.autumn.common.api.application.dto.input.IStatusInput;
import com.autumn.util.AutoMapUtils;
import com.autumn.util.DataRollbackException;
import com.autumn.util.ExceptionUtils;
import com.autumn.zero.application.services.AbstractZeroQueryAppService;
import com.autumn.zero.workflow.application.dto.definitions.UpdateProcessContentInput;
import com.autumn.zero.workflow.application.dto.definitions.WorkflowDefinitionInput;
import com.autumn.zero.workflow.application.dto.definitions.WorkflowDefinitionOutput;
import com.autumn.zero.workflow.application.services.IWorkflowDefinitionAppService;
import com.autumn.zero.workflow.constants.ProcessDesignStatusConstants;
import com.autumn.zero.workflow.entitys.WorkflowDefinition;
import com.autumn.zero.workflow.repositorys.IWorkflowDefinitionRepository;
import com.autumn.zero.workflow.services.IWorkflowDefinitionService;

/**
 * 工作流定义应用服务实现
 * 
 * @author 杨昌国 2019-01-11 13:56:50
 */
public class WorkflowDefinitionAppServiceImpl extends
		AbstractZeroQueryAppService<Long, WorkflowDefinition, IWorkflowDefinitionRepository, WorkflowDefinitionOutput>
		implements IWorkflowDefinitionAppService {

	@Autowired
	private IWorkflowDefinitionRepository repository;

	@Autowired
	private IWorkflowDefinitionService service;

	@Override
	public String getModuleName() {
		return "工作流定义";
	}

	@Override
	protected IWorkflowDefinitionRepository getQueryRepository() {
		return this.repository;
	}

	private Set<String> searchMembers = new HashSet<>();

	@Override
	protected Set<String> getSearchMembers() {
		if (searchMembers.size() == 0) {
			searchMembers.add(WorkflowDefinition.FILED_FLOW_NAME);
		}
		return searchMembers;
	}

	@Override
	@Transactional(rollbackFor = DataRollbackException.class)
	public void updateByStatus(IStatusInput<Long> input) {
		ExceptionUtils.checkNotNull(input, "input");
		service.updateByStatus(input.getId(), input.getStatus());
	}

	@Override
	@Transactional(rollbackFor = DataRollbackException.class)
	public WorkflowDefinitionOutput add(WorkflowDefinitionInput input) {
		ExceptionUtils.checkNotNull(input, "input");
		input.valid();
		WorkflowDefinition entity = AutoMapUtils.map(input, WorkflowDefinition.class);
		entity.setStatus(ProcessDesignStatusConstants.NOT_PUBLISH);
		service.add(entity);
		return AutoMapUtils.map(entity, WorkflowDefinitionOutput.class);
	}

	@Override
	@Transactional(rollbackFor = DataRollbackException.class)
	public WorkflowDefinitionOutput update(WorkflowDefinitionInput input) {
		ExceptionUtils.checkNotNull(input, "input");
		input.valid();
		WorkflowDefinition entity = this.repository.get(input.getId());
		if (entity == null) {
			ExceptionUtils.throwValidationException("无法修改不存在的流程定义");
		}
		AutoMapUtils.map(input, entity);
		entity.setStatus(ProcessDesignStatusConstants.NOT_PUBLISH);
		service.update(entity);
		return AutoMapUtils.map(entity, WorkflowDefinitionOutput.class);
	}

	@Override
	public WorkflowDefinitionOutput queryById(Long id) {
		WorkflowDefinition entity = service.queryById(id);
		if (entity == null) {
			return null;
		}
		return AutoMapUtils.map(entity, WorkflowDefinitionOutput.class);
	}

	@Override
	@Transactional(rollbackFor = DataRollbackException.class)
	public void deleteById(Long id) {
		service.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = DataRollbackException.class)
	public void updateByProcessContent(UpdateProcessContentInput input) {
		ExceptionUtils.checkNotNull(input, "input");
		input.valid();
		service.updateByProcessContent(input.getId(), input.getXmlContent());
	}

}
